package com.fjwt.gz.proxy.ctrl.score.rule;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.fjwt.gz.core.constants.Constants;
import com.fjwt.gz.core.model.ApiRes;
import com.fjwt.gz.db.entity.*;
import com.fjwt.gz.db.vo.AgentScoreRuleVO;
import com.fjwt.gz.db.vo.ScoreRuleAndTotalVO;
import com.fjwt.gz.proxy.ctrl.CommonCtrl;
import com.fjwt.gz.service.impl.AgentScoreRuleService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 *  积分规则接口类
 */
@RestController
@RequestMapping("/api/scoreRule")
@Slf4j
public class ScoreRuleController extends CommonCtrl {

    @Autowired
    private AgentScoreRuleService agentScoreRuleService;


    @PostMapping("/getSysConfig")
    public ApiRes getSysConfig(){
        LambdaQueryWrapper<SysConfig> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SysConfig::getGroupKey, Constants.POINT.POINT_PRODUCE);
        List<SysConfig> list = sysConfigService.getBaseMapper().selectList(queryWrapper);
        List<SysConfig> listArr = new ArrayList<>();
        listArr.addAll(list);
        return ApiRes.ok(listArr);
    }

    /**
     * 积分规则列表
     */
    @RequestMapping(value="", method = RequestMethod.GET)
    public ApiRes getScoreRule(){
        AgentScoreRuleVO agentScoreRuleVO = getObject(AgentScoreRuleVO.class);
        log.info("proxy端积分规则列表，请求地址：/api/scoreRule，请求参数：{}", JSONObject.toJSONString(agentScoreRuleVO));
        agentScoreRuleVO.setSysType(Constants.SYS_ROLE_TYPE.PROXY);
        agentScoreRuleVO.setCurrentCreatedAgentNo(getCurrentAgentNo());
        agentScoreRuleVO.setAgentNo(checkAgentAuth(getCurrentAgentNo(), agentScoreRuleVO.getAgentNo()));
        IPage<ScoreRuleAndTotalVO> page = agentScoreRuleService.findScoreRuleLists(getIPage(), agentScoreRuleVO);
        log.info("proxy端积分规则列表，请求地址：/api/scoreRule，返回结果：{}", JSONObject.toJSONString(page));
        return ApiRes.page(page);
    }

    /**
     * 获取积分规则详情
     */
    @PreAuthorize("hasAnyAuthority('ENT_SCORE_RULE_INFO_VIEW')")
    @PostMapping("/getInfo")
    public ApiRes getInfo(){
        String scoreRuleId = getValString("scoreRuleId");
        log.info("proxy端积分规则详情，请求地址：/api/scoreRule/getInfo，请求参数：{}", scoreRuleId);
        ScoreRuleAndTotalVO vo = agentScoreRuleService.queryRuleAndTotalByRuleId(scoreRuleId);
        log.info("proxy端积分规则详情，请求地址：/api/scoreRule/getInfo，返回结果：{}", JSON.toJSONString(vo));
        return ApiRes.ok(vo);
    }

    /**
     * 积分规则新增
     */
    @PreAuthorize("hasAnyAuthority('ENT_SCORE_RULE_INFO_ADD')")
    @PostMapping("/save")
    public ApiRes scoreRuleSave(){
        AgentScoreRuleEntity entity = getObject(AgentScoreRuleEntity.class);
        log.info("机构端积分规则新增，请求地址：/api/scoreRule/save，请求参数：{}", JSON.toJSONString(entity));
        int result = agentScoreRuleService.scoreRuleSave(entity);
        if(result > 0){
            return ApiRes.ok();
        }else{
            return ApiRes.customFail("操作失败！");
        }
    }

}
